home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Files / FormatAsDOS / FormatAsDOSDisk.c next >
Encoding:
C/C++ Source or Header  |  1996-08-09  |  3.9 KB  |  126 lines  |  [TEXT/CWIE]

  1. /******************************************************************************************
  2.     FormatAsDOSDisk.c
  3.  
  4.     Written by Jim Luther with some modifications by Virginia McCulloh
  5.     Apple Developer Technical Support
  6.     August, 1996, Cupertino, CA
  7.     Copyright 1996, Apple Computer, Inc.
  8.     
  9.     This snippet demonstrates how to use the newer Disk Initialization
  10.     Package calls DIXFormat() and DIXZero() to format a floppy disk as
  11.     a DOS disk.  DIXFormat, DIXZero, and DIReformat are documented in 
  12.     the IM:Files Errata Tech Note.
  13.     
  14.     Please note this snippet does not provide any prompts.  It merely takes
  15.     the floppy in the first floppy drive and reformats it as a DOS disk with 
  16.     the name "MS-DOS Disk".  This purpose of this snippet is to demonstrate
  17.     the newer API.
  18.     
  19.     This runs under Metrowerks CodeWarrior 8 with the Universal 
  20.     Interfaces 2.1 from ETO #18.
  21.  
  22. /*****************************************************************************/
  23.  
  24. #include <Gestalt.h>
  25. #include <FSM.h>
  26. #include <DiskInit.h>
  27.  
  28. /******************************************************************************/
  29.  
  30. /*
  31. **    Prototypes and inlines for newer Disk INIT calls
  32. */
  33.  
  34. extern pascal OSErr DIXFormat(short drvNum, Boolean fmtFlag, unsigned long fmtArg, 
  35.                           unsigned long *actSize)
  36.         THREEWORDINLINE(0x700C, 0x3F00, 0xA9E9);
  37.  
  38. extern pascal OSErr DIXZero(short drvNum, ConstStr255Param volName, short fsid,
  39.                         short mediaStatus, short volTypeSelector,
  40.                         unsigned long volSize, void *extendedInfoPtr)
  41.         THREEWORDINLINE(0x700E, 0x3F00, 0xA9E9);
  42.  
  43. extern pascal OSErr DIReformat(short drvNum, short fsid, ConstStr255Param volName,
  44.                            ConstStr255Param msgText)
  45.         THREEWORDINLINE(0x7010, 0x3F00, 0xA9E9);
  46.  
  47. /*
  48. **    Prototypes for functions here
  49. */
  50.  
  51. Boolean  HasExtendedDIFunctions(void);
  52. OSErr    FormatAsDOS(short driveNumber, ConstStr255Param volName);
  53.  
  54.  
  55. /******************************************************************************/
  56.  
  57. /*
  58. **    See if new extended Disk Init package is available
  59. */
  60. Boolean  HasExtendedDIFunctions(void)
  61. {
  62.     long response;
  63.     
  64.     if ( Gestalt(gestaltFSAttr, &response) == noErr )
  65.         return ( (response & (1L << gestaltHasExtendedDiskInit)) != 0 );
  66.     else
  67.         return ( false );
  68. }
  69.  
  70. /******************************************************************************/
  71.  
  72. /*
  73. **    Format the unmounted floppy disk specified by driveNum as an MS-DOS disk.
  74. */
  75. OSErr    FormatAsDOS(short driveNumber, ConstStr255Param volName)
  76. {
  77.     enum
  78.     {
  79.         kMacPCExchangeFSID = 0x4953    /* file system ID of Macintosh PC Exchange's
  80.                                         MS-DOS file system (see "Guide-File System Mgr" 
  81.                                         on MacOS SDK in File System Manager SDK).*/
  82.     };
  83.     OSErr            result;
  84.     unsigned long    actSize;
  85.     
  86.     /*
  87.     **    Format the disk. Passing false for fmtFlag and 1440 for fmtArg tells
  88.     **    the Disk Init package to format the disk with 1440 blocks (720K MFM) or
  89.     **    if 1440 blocks isn't available (because the disk is a high density disk),
  90.     **    to format the disk with the smallest size that's greater than 1440 blocks
  91.     **    (which will be 2880 blocks (1440K MFM)).
  92.     */
  93.     result = DIXFormat(driveNumber, false, 1440, &actSize);
  94.     if ( result == noErr )
  95.     {
  96.         /* Verify the disk */
  97.         result = DIVerify(driveNumber);
  98.         
  99.         /*
  100.         **    The result of DIVerify is passed to DIXZero in the mediaStatus parameter.
  101.         **    If mediaStatus is noErr, then the disk is simply initialized.  If
  102.         **    mediaStatus is not noErr, then DIXZero attempts to spare any bad blocks
  103.         **    if the file system supports that. MS-DOS disks can be bad block spared.
  104.         */
  105.         result = DIXZero(driveNumber, volName, kMacPCExchangeFSID, result,
  106.                          0, actSize, NULL);
  107.     }
  108.     
  109.     return ( result );
  110. }
  111.  
  112. /******************************************************************************/
  113.  
  114. /* Test code */
  115. void    main(void)
  116. {
  117.     short            driveNumber = 1;    /* hard coded to use the 1st floppy drive */
  118.     OSErr            result;
  119.     Str255            volName = "\pMS-DOS Disk";
  120.     
  121.     if ( HasExtendedDIFunctions() )
  122.     {
  123.         result = UnmountVol(NULL, driveNumber);    /* unmount disk in the drive (if it was mounted) */
  124.         result = FormatAsDOS(driveNumber, volName);
  125.     }
  126. }